//
//  PMineAccountSettingViewController.m
//  Btpin
//
//  Created by Vic on 2019/3/23.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PMineAccountSettingViewController.h"
#import "LYSettingCell.h"
#import "DataToolsControl.h"

#import "LYSettingchangeMobileController.h"
#import "LYSettingUsernameController.h"
#import "LYSettingChanagePasswordController.h"
#import "AboutUsViewController.h"
#import "AppDelegate.h"

@interface PMineAccountSettingViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *dataArray;
}
@property (nonatomic, strong) UITableView *tableview;

@end

@implementation PMineAccountSettingViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"账号设置";
    
    NSString *mobile = UD_GetOForK(isMobile);
    if (mobile.length>0) {
        mobile = [NSString stringWithFormat:@"%@", mobile];
    } else {
        mobile = @"";
    }
    NSString *name = UD_GetOForK(isUserName);
    if (name.length>0) {
    } else {
        name = @"";
    }
//    NSString *versoinStr = [UIToolsControl getVersion];
//    dataArray = @[@{@"name":@"修改手机号", @"detail":mobile},
//                  @{@"name":@"设置用户名", @"detail":name},
//                  @{@"name":@"修改密码", @"detail":@""},
//                  @{@"name":@"当前版本", @"detail":versoinStr}];
    
    dataArray = @[@{@"name":@"修改手机号", @"detail":mobile},
                  @{@"name":@"设置用户名", @"detail":name},
                  @{@"name":@"修改密码", @"detail":@""},
                  @{@"name":@"关于我们", @"detail":@""}];
    
    [self initUI];
    [self requestData];
}
- (void)initUI {
    UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height) style:UITableViewStyleGrouped];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.showsVerticalScrollIndicator = NO;
    tableview.estimatedRowHeight = 0;
    tableview.estimatedSectionHeaderHeight = 0;
    tableview.estimatedSectionFooterHeight = 0;
    tableview.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    tableview.sectionIndexBackgroundColor = [UIColor clearColor];
    self.tableview = tableview;
    [tableview registerClass:[LYSettingCell class] forCellReuseIdentifier:kCellIdentifier_LYSettingCell];
    [self.view addSubview:tableview];
    
    UIButton *footBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    footBtn.frame = CGRectMake( 0, 0, 80, 45);
    footBtn.backgroundColor = [UIColor whiteColor];
    [footBtn setTitle:@"退出当前账户" forState:UIControlStateNormal];
    [footBtn setTitleColor:THEMECOLOR_OF_RED forState:UIControlStateNormal];
    footBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [footBtn addTarget:self action:@selector(footBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    tableview.tableFooterView = footBtn;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LYSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_LYSettingCell forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    NSDictionary *dic = dataArray[indexPath.row];
    [cell setTitle:dic[@"name"] detail:dic[@"detail"]];
    return cell;
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    __weak typeof(self) weakSelf = self;
    if (indexPath.row == 0) {
        LYSettingchangeMobileController *mobileVC = [[LYSettingchangeMobileController alloc] init];
        NSString *mobile = UD_GetOForK(isMobile);
        if (mobile.length>0) {
            mobile = [NSString stringWithFormat:@"%@",mobile];
        } else {
            mobile = @"";
        }
        mobileVC.mobile = mobile;
        mobileVC.block = ^(){
            [weakSelf.tableview reloadData];
        };
        [self.navigationController pushViewController:mobileVC animated:YES];
    } else if (indexPath.row == 1) {
        LYSettingUsernameController *userVC = [[LYSettingUsernameController alloc] init];
        NSString *name = UD_GetOForK(isUserName);
        if (name.length>0) {
        } else {
            name = @"";
        }
        userVC.name = name;
        userVC.block = ^{ // 用户名修改回调
            [weakSelf.tableview reloadData];
        };
        [self.navigationController pushViewController:userVC animated:YES];
    } else if (indexPath.row == 2)  {
        LYSettingChanagePasswordController *passwordVC = [[LYSettingChanagePasswordController alloc] init];
        passwordVC.isExistPWD = YES;
        [self.navigationController pushViewController:passwordVC animated:YES];
    } else if (indexPath.row == 3) {
        AboutUsViewController *AVC = [[AboutUsViewController alloc] init];
        [self.navigationController pushViewController:AVC animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.00001;
}

#pragma mark --loadData
- (void)requestData{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [BaseNetManager requestWithMethod:POST path:MyBaseInfo_Get_URL params:dic successBlock:^(NSDictionary *responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            [DataToolsControl dataAnalysisUserBaseInfo:responseObject];
            [self.tableview reloadData];
        }
    } failurBlock:^(NSError *error) {
        SVPHUDError(NetErrorAlertText);
    }];
}

#pragma mark -- Action
- (void)footBtnClick:(UIButton *)button{
    [DataToolsControl deallocUserData];
    SVPHUDSuccess(@"退出成功");
    
    [self performSelector:@selector(signoutClick) withObject:nil afterDelay:0.5];
}
- (void)signoutClick {
    [SharedApp showViewController:TabBarTypeOfSignin];
    if ([DataToolsControl sharedManager].isTIMLoginSuccess) {
        [[TIMManager sharedInstance] logout:^() {
            NSLog(@"logout succ");
        } fail:^(int code, NSString * err) {
            NSLog(@"logout fail: code=%d err=%@", code, err);
        }];
    }
}

@end
